<?php
/**
 * White Page Web Crawling
 */

use Fox\Common\Localization\SubRegion;
use Fox\Common\Localization\City;
use Fox\Common\Localization\City\Street;
use Fox\Html\CheckBox;
use Fox\Html\Input;
use Fox\Html\Screen;

set_time_limit(Xview::TIMEOUT_VERY_LONG);

$screen = new Screen("extract","extract");
$block = new Block("b","Extract Source Files");
$block->add(new TextArea("postal_code",true));
$block->add(new CheckBox("commit"));
$block->add(new Input("dbcnt",true,1));
$screen->add($block);

if($screen->posted())
{
	$grid = new Fox\Grid("gResult");
	$grid->setFieldCat(array("cityid","cityname","postal_code","streetid","streetname","msgtx"));
	$inputs = $screen->get();
	
	if(!Xview::instance()->errors())
	{

		if(!empty($inputs['postal_code']))
		{
			$sql = null;
			$zipcodes = preg_split("/,/", $inputs['postal_code']);
			foreach($zipcodes as $zipcode)
			{
				if(empty($sql))
					$sql = " postal_code LIKE '$zipcode%' ";
				else
					$sql .= "OR postal_code LIKE '$zipcode%' ";
			}
			$fragments[] = new Fox\Dao\SqlFragment($sql);
		}
		$fragments[] = new Fox\Dao\SqlFragment("disable = 0 AND streets_status = 0");
		$fragment = Fox\Dao\SqlFragment::combine($fragments);
		$finder = new City();
		$cities = $finder->find($fragment,array("keyname" => false),null,$inputs["dbcnt"]);
		
		push_msg("I","Nb City Loaded :".count($cities));
		$errors = array();
		// Find City
		foreach($cities as $city)
		{
			$stNb = 0;
			
			// Find Corresponding Streets
			foreach($city->streets() as $street)
			{		
				/* @var $street Street */
				$save = false;
				$status = Street::STATUS_LOADED;
				/* Check First if Street has not been already fully loaded */
				if($street->status == 1)
				{
					continue;
				}
				
				$name = $street->name();
				$ficnm = _TEMP_.$city->cityid."-".$street->streetid.".html";
				if(!is_file($ficnm))
				{
					$errors[] = array("cityid" => $city->cityid,
									  "cityname" => $city->fullname,
									  "postal_code" => $city->postal_code,
									  "streetid" => $street->streetid,
									  "streetname" => $name,
									  "msgtx" => "No such file in directory");
				}
				else
				{
					$html = file_get_html($ficnm);
					$visitCards = $html->find(".visitCard");
					if(count($visitCards) == 0)
					{
						// Define if its a issue of in data extraction 
						// from the source or if the request return a zero
						// result response
						$errorPage = $html->find("div.errorPageMessage");
						if(count($errorPage) > 0)
						{
							$errors[] = array("cityid" => $city->cityid,
											  "cityname" => $city->fullname,
											  "postal_code" => $city->postal_code,
											  "streetid" => $street->streetid,
											  "streetname" => $name,
											  "msgtx" => $errorPage[0]->plantext);
							$status = Street::STATUS_ERROR_DURING_EXTRACION;
						}
						else
						{
							$errors[] = array("cityid" => $city->cityid,
									  "cityname" => $city->fullname,
									  "postal_code" => $city->postal_code,
									  "streetid" => $street->streetid,
									  "streetname" => $name,
									  "msgtx" => "No result found in the source");
							$status = Street::STATUS_NO_SOURCE_INFO;
						}
					}
					else
					{
						foreach($visitCards as $card)
						{
							
							// Retrieve First and Last Name
							$nameObject = $card->find("h2 a");
							$fullname = utf8_decode(trim($nameObject[0]->plaintext));
							$fields = null;
							$fields['fullname'] = $fullname;
							// Localisation
							$localisationObject = $card->find("div.localisationBlock p");
							$localisation = utf8_decode(trim($localisationObject[0]->plaintext));
							$fields['localisation'] = $localisation;
							// Contact Part
							$contactObjects = $card->find("div.contactBlock li");
							foreach($contactObjects as $contactParts)
							{
								$type = null;
								if(preg_match("/\<a/", $contactParts->innertext))
								{
									$subparts = preg_split("/\<a/",$contactParts->innertext);
									if(isset($subparts[0]))
									{
										$type = trim($subparts[0]);
									}
								}
						
								$numbers = $contactParts->find("strong");
								if(isset($numbers[0]))
								{
									$fields['phones'][] = array('type' => $type ,'number' => trim($numbers[0]->plaintext));
								}
							}
							
							var_dump($fields);
						}
						
						
					}
					
				}

				if($save)
				{
					$street->setFields(array("status" => 1));
					try {
						//$street->update();
						push_msg("S","City $city->fullname / Street $name has been loaded.");
					}
					catch(Fox\Object\Exception\Save $e)
					{
						push_msg("E",$e->getMessage());
					}
				}
			}
		}
		$grid->setDatas($errors);
	}
}
echo $screen->display();
if(isset($grid))
	echo $grid->display();